//
//  FinancialParities.m
//  MINFOBBS
//
//  Created by juvid on 13-11-29.
//  Copyright (c) 2013年 minfo. All rights reserved.
//

#import "FinancialParities.h"
#import "UIColor+expanded.h"
#import "SBJson.h"
#import "DetectionSystemVersion.h"
@interface FinancialParities ()

@end

@implementation FinancialParities

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    NSDate *  senddate=[NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *  locationString=[dateformatter stringFromDate:senddate];
    self.labTime.text=[NSString stringWithFormat:@"%@ 中国银行",locationString];
    [self GetDatas];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewWillDisappear:(BOOL)animated{
    [tRequest clearDelegatesAndCancel];
    [tRequest cancel];
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [NSThread cancelPreviousPerformRequestsWithTarget:self];
}
#pragma mark 异步数据请求
-(void)GetDatas{
    tRequest=[[[ScreenAccess alloc]init]ASIGetSearch:self.strKey Start:4  Limit:self.pageLimit];
    [tRequest setDelegate:self];
    [tRequest setUsername:@"allMessage"];
    
}
-(void)requestFinished:(ASIHTTPRequest*)request{
    
    NSString *response = [request responseString];
    SBJsonParser * parser = [[SBJsonParser alloc] init];
    NSMutableDictionary *Result = [parser objectWithString:response];
    
    if ([request.username isEqual:@"allMessage"]){
        BOOL IsSuccess=[[[Result objectForKey:@"Response"]objectForKey:@"status"]integerValue]==200?YES:NO;
        if (IsSuccess==YES) {
            if (self.arrReturn==nil) {
                self.arrReturn=[[NSMutableArray alloc]init];
            }
//            [[[arr objectAtIndex:j] objectForKey:@"Items"] objectForKey:@"Item"]
            [self.arrReturn addObjectsFromArray: [[[[[[Result objectForKey:@"Response"] objectForKey:@"Categories"] objectForKey:@"Category"] objectAtIndex:0]objectForKey:@"Items"] objectForKey:@"Item"]];
           
            [self.tableView reloadData];
            
        }
    }
}

-(void)requestFailed:(ASIHTTPRequest*)request{
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
       return self.arrReturn.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    NSArray *arr=[[[self.arrReturn objectAtIndex:indexPath.row] objectForKey:@"Properties"] objectForKey:@"Property"];
    NSString *strIcon=[[self.arrReturn objectAtIndex:indexPath.row] objectForKey:@"Title"];
//    NSRange rangeIcon = [strIcon rangeOfString:@"\t"];
//    NSLog(@"%d",rangeIcon.location);
    strIcon=[strIcon substringToIndex:2];
    cell.imageView.image=[UIImage imageNamed:strIcon];
    if ([[DetectionSystemVersion alloc]Version]) {
        cell.textLabel.font=[UIFont systemFontOfSize:12];
    }
    else{
    cell.textLabel.font=[UIFont systemFontOfSize:14];
    }
    cell.textLabel.numberOfLines=0;
//    cell.textLabel.textColor=[UIColor colorWithHexString:@"0x4a4a4a"];
    if (arr.count>=2) {
         cell.textLabel.text=[NSString stringWithFormat:@"%@\n%@",[[arr objectAtIndex:0] objectForKey:@"text"],[[arr objectAtIndex:1] objectForKey:@"text"]];
    }
    else{
        NSString *str=[[arr objectAtIndex:0] objectForKey:@"text"];
        NSRange range = [str rangeOfString:@"\n 中国银行"];
        str=[str substringToIndex:range.location];
         cell.textLabel.text=[NSString stringWithFormat:@"%@",str];
    }
      // Configure the cell...
    cell.textLabel.backgroundColor=[UIColor clearColor];
    if (indexPath.row%2==1) {
        UIView *vie=[[UIView alloc]init];
        vie.backgroundColor=[UIColor colorWithHexString:@"0xf3f3f3"];
        cell.backgroundView=vie;
    }
    else{
        cell.backgroundColor=[UIColor whiteColor];
    }
    return cell;
}
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
-(float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 60;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *result;
    result=[[UIView alloc]init];
    result.backgroundColor=[UIColor clearColor];
    
    [result addSubview:self.vieHead];
//    if (self.isSelf) {
       //    }
    return result;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];

    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
 
 */

@end
